package GEO;
import GdUtil.HttpClientTest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;
import org.junit.Test;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;

/**
 * @Author MJS
 * @Date 2021/12/15 16:22
 * @Description
 */
public class GeoTest {

    public static void main(String[] args) {
        String sendAddress ="河北省石家庄市";
        String receiveAddress = "北京市";
        String path ="https://restapi.amap.com/v3/geocode/geo";

        MultiValueMap<String, String> sendParams = new LinkedMultiValueMap();
        sendParams.add("key", "388d904938aae4c2fa2dfaf666a41dd7");
        sendParams.add("address", sendAddress);
        URI sendUrl = UriComponentsBuilder.fromHttpUrl(path).queryParams(sendParams).build().toUri();
        String s = sendUrl.toString();
        String send = HttpClientTest.doPostJsonOne(s);
        AddressInfoBo sendAddressBo = JSON.parseObject(send, AddressInfoBo.class);
        String sendLocation = sendAddressBo.getGeocodes().get(0).getLocation();
        String[] sendSplit = sendLocation.split(",");



        MultiValueMap<String, String> receiveParams = new LinkedMultiValueMap();
        receiveParams.add("key", "388d904938aae4c2fa2dfaf666a41dd7");
        receiveParams.add("address", receiveAddress);
        URI receiveUrl = UriComponentsBuilder.fromHttpUrl(path).queryParams(receiveParams).build().toUri();
        String s1 = receiveUrl.toString();
        String receive = HttpClientTest.doPostJsonOne(s1);
        AddressInfoBo receiveAddressBo = JSON.parseObject(receive, AddressInfoBo.class);
        String receiveLocation = receiveAddressBo.getGeocodes().get(0).getLocation();
        String[] receiveSplit = receiveLocation.split(",");

        GlobalCoordinates sendGlobalCoordinates = new GlobalCoordinates(Double.parseDouble(sendSplit[0]) , Double.parseDouble(sendSplit[1]));
        GlobalCoordinates receiveGlobalCoordinates = new GlobalCoordinates(Double.parseDouble(receiveSplit[0]), Double.parseDouble(receiveSplit[1]));

        GeodeticCurve geodeticCurve = new GeodeticCalculator().calculateGeodeticCurve(Ellipsoid.WGS84, sendGlobalCoordinates, receiveGlobalCoordinates);
        Double ellipsoidalDistance = geodeticCurve.getEllipsoidalDistance();
        double azimuth = geodeticCurve.getAzimuth();
        System.out.println("米："+ellipsoidalDistance.longValue());
        System.out.println("度："+azimuth);

    }
    @Test
    public void geoTest(){
        String sendAddress ="河北省石家庄市";
        String receiveAddress = "北京市";
        String path ="https://restapi.amap.com/v3/geocode/geo";
        String path2 = "https://restapi.amap.com/v3/direction/driving";

        MultiValueMap<String, String> sendParams = new LinkedMultiValueMap();
        sendParams.add("key", "388d904938aae4c2fa2dfaf666a41dd7");
        sendParams.add("address", sendAddress);
        URI sendUrl = UriComponentsBuilder.fromHttpUrl(path).queryParams(sendParams).build().toUri();
        String s = sendUrl.toString();
        String send = HttpClientTest.doPostJsonOne(s);
        AddressInfoBo sendAddressBo = JSON.parseObject(send, AddressInfoBo.class);
        String sendLocation = sendAddressBo.getGeocodes().get(0).getLocation();
        String[] sendSplit = sendLocation.split(",");

        MultiValueMap<String, String> receiveParams = new LinkedMultiValueMap();
        receiveParams.add("key", "388d904938aae4c2fa2dfaf666a41dd7");
        receiveParams.add("address", receiveAddress);
        URI receiveUrl = UriComponentsBuilder.fromHttpUrl(path).queryParams(receiveParams).build().toUri();
        String s1 = receiveUrl.toString();
        String receive = HttpClientTest.doPostJsonOne(s1);
        AddressInfoBo receiveAddressBo = JSON.parseObject(receive, AddressInfoBo.class);
        String receiveLocation = receiveAddressBo.getGeocodes().get(0).getLocation();
        String[] receiveSplit = receiveLocation.split(",");


        MultiValueMap<String, String> geoParams = new LinkedMultiValueMap();
        geoParams.add("key","388d904938aae4c2fa2dfaf666a41dd7");
        geoParams.add("origin",sendLocation);
        geoParams.add("destination",receiveLocation);
        geoParams.add("nosteps","1");
        URI geoUrl = UriComponentsBuilder.fromHttpUrl(path2).queryParams(geoParams).build().toUri();
        String s2 = geoUrl.toString();
        String geo = HttpClientTest.doPostJsonOne(s2);


        JSONObject jsonObject = JSON.parseObject(geo);
        DistancePojoV2 distancePojo = (DistancePojoV2) JSONObject.toJavaObject(jsonObject, DistancePojoV2.class);
        Long distance = Long.valueOf(distancePojo.getRoute().getPaths().get(0).getDistance());
        System.out.println("米："+distance);
    }

    @Test
    public void v1() {
        int[] ints = new int[11];
        ints[0] = 1;
        ints[1] = 2;
        System.out.println(f(ints,10));
    }

    public Integer f(int[] x,int a){

        if (x[a] != 0) return x[a];

        x[a]=f(x,a-1)+f(x,a-2);
        return x[a];
    }

    @Test
    public void v2() {
       int a=0;
       int b=1;
       int p;
       for (int i=0;i<9;++i){
           p=b;
           b=a+b;
           a=p;
       }
        System.out.println(b);
    }
}
